﻿using KPDownloadAutoTask;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;


namespace Jmit.Deyi.AutoTask
{
    public class AppConfigManager
    {

        public static int trancountid = 0;


        public static AppConfig appConfig = new AppConfig();
        public static AppConfig LoadConfig()
        {
            
            appConfig.dbConn_from = System.Configuration.ConfigurationManager.AppSettings["dbConn_from"].ToString();
            appConfig.dbConn_to = System.Configuration.ConfigurationManager.AppSettings["dbConn_to"].ToString();
            appConfig.fieldName = System.Configuration.ConfigurationManager.AppSettings["fieldName"].ToString();
            appConfig.userName = System.Configuration.ConfigurationManager.AppSettings["userName"].ToString();
            appConfig.tableName = System.Configuration.ConfigurationManager.AppSettings["tableName"].ToString();
            appConfig.trancountid = System.Configuration.ConfigurationManager.AppSettings["trancountid"].ToString();
            appConfig.interval = System.Configuration.ConfigurationManager.AppSettings["interval"].ToString();
            appConfig.hour = System.Configuration.ConfigurationManager.AppSettings["hour"].ToString();
            appConfig.minute = System.Configuration.ConfigurationManager.AppSettings["minute"].ToString();
            appConfig.version = System.Configuration.ConfigurationManager.AppSettings["version"].ToString();

            return appConfig;

            /*else
            {
                SaveConfig();
            }*/
        }

        public static void SaveConfig(AppConfig _deyiConfig,string path = "")
        {
            //获取Configuration对象
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //写入元素的Value
            config.AppSettings.Settings["dbConn_from"].Value = appConfig.dbConn_from;
            config.AppSettings.Settings["dbConn_to"].Value = appConfig.dbConn_to;
            config.AppSettings.Settings["fieldName"].Value = appConfig.fieldName;
            config.AppSettings.Settings["userName"].Value = appConfig.userName;
            config.AppSettings.Settings["tableName"].Value = appConfig.tableName;
            config.AppSettings.Settings["interval"].Value = appConfig.interval;
            config.AppSettings.Settings["hour"].Value = appConfig.hour;
            config.AppSettings.Settings["minute"].Value = appConfig.minute;
            config.AppSettings.Settings["version"].Value = appConfig.version;
            
            //增加元素
            //config.AppSettings.Settings.Add("url", "http://www.myhack58.com");
            //删除元素
            //config.AppSettings.Settings.Remove("name");
            
            //一定要记得保存，写不带参数的config.Save()也可以
            config.Save(ConfigurationSaveMode.Modified);
            //刷新，否则程序读取的还是之前的值（可能已装入内存）
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
        }



    }
}
